package com.mls.guardian.common;

import com.caucho.hessian.io.*;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * Created by jl on 15/9/10.
 */
public class SerializationUtils {
    static Kryo kryo = new Kryo();
    static{
        kryo.setReferences(true);
    }
    public static byte[] kryoSerizlize(Object obj) {
        byte[] buffer = new byte[2048];
        try(
                Output output = new Output(buffer);
        ) {

            kryo.writeClassAndObject(output, obj);
            return output.toBytes();
        } catch (Exception e) {
        }
        return buffer;
    }


    public static Object kryoUnSerizlize(byte[] src) {
        try(
                Input input = new Input(src);
        ){
            return kryo.readClassAndObject(input);
        }catch (Exception e) {
        }
        return kryo;
    }
    public static byte[] serialize(Object obj) throws IOException {
        if(obj==null) throw new NullPointerException();

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        HessianOutput ho = new HessianOutput(os);
        ho.writeObject(obj);
        return os.toByteArray();
    }

    public static Object deserialize(byte[] by) throws IOException{
        if(by==null) throw new NullPointerException();

        ByteArrayInputStream is = new ByteArrayInputStream(by);
        HessianInput hi = new HessianInput(is);
        return hi.readObject();
    }

    public static byte[] H2Serialize(Object obj) throws IOException {
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(2048);

        Hessian2Output hessianOutput = new Hessian2Output(byteBuffer);

        hessianOutput.setSerializerFactory(reponseSerializerFactory);

        hessianOutput.writeObject(obj);

        hessianOutput.flush();

        byte[] bytes = byteBuffer.toByteArray();
        return bytes;
    }
    static SerializerFactory reponseSerializerFactory = new SerializerFactory();

    public static Object H2Deserialize(byte[] bytes,Class cls) throws IOException {

        ByteArrayInputStream input = new ByteArrayInputStream(bytes);

        Hessian2Input hessianInput = new Hessian2Input(input);

        hessianInput.setSerializerFactory(reponseSerializerFactory);
        Object obj = hessianInput.readObject(cls);
        return obj;
    }

    public static void main(String a[]) throws IOException {

//        // //类的模式设置为Person类
//        TWmsInventoryLogEntity logEntity = new TWmsInventoryLogEntity();
//        logEntity.setOrderId(1l);
//        logEntity.setTypeCode("typeCode");
////        logEntity.setOrderKey("orderKey");
//        logEntity.setQty(100);
//        logEntity.setSkuId(10030010l);
//        System.out.println(JsonUtil.serialize(logEntity).getBytes().length);
//        System.out.println(serialize(logEntity).length);
//        TWmsInventoryLogEntity logEntity1 = (TWmsInventoryLogEntity) deserialize(serialize(logEntity));
//        System.out.println();

//        byte bits[] = kryoSerizlize(logEntity);
//        TWmsInventoryLogEntity inventoryLogEntity = (TWmsInventoryLogEntity) kryoUnSerizlize(bits);
//        System.out.println(1);
//


//
//        MessageMapSchema m =  new MessageMapSchema(RuntimeSchema.getSchema(String.class),RuntimeSchema.getSchema(Object.class));
//        byte[] bytes = ProtobufIOUtil.toByteArray(map1, m, buffer);
//        Map map2 = new HashMap();
//        ProtobufIOUtil.mergeFrom(bytes, map2, m);
//        System.out.println();
    }
}
